package org.familysearch.fsapi.gwt.server.dao.rest;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;
import org.familysearch.fsapi.gwt.common.client.entities.Person;
import org.familysearch.fsapi.gwt.common.client.entities.Temple;
import org.familysearch.fsapi.gwt.server.dao.DAOException;
import org.familysearch.fsapi.gwt.server.dao.PersonDAO;
import org.familysearch.fsapi.gwt.server.dao.rest.parsers.PersonSAXParser;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class PersonDAOREST
		extends GenericRESTDAO<Temple, String>
		implements PersonDAO
{

	private Logger log;
	
	public PersonDAOREST()
	{
		log = Logger.getLogger(this.getClass().getName());
	}

	public List<Person> findAll(boolean ascending) throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}


	public List<Person> findAll(int maxResults, boolean ascending) throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}


	public List<Person> findAll(String sortBy, boolean ascending) throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}


	public List<Person> findAll(int maxResults, String sortBy, boolean ascending) throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}


	public List<Person> findAll(Person loaded) throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}


	public List<Person> findByExample(Person exampleInstance, String... excludeProperty) throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Person> findAll() throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	public Person findById(String id) throws DAOException {

		/*
		 * The calling BO should always have authenticated before
		 * coming down to an entity DAO, so we know that
		 * the parent DAO class has a sessionID
		 */
		PersonSAXParser parser = new PersonSAXParser();
		
		if( session == null )
		{
			throw new DAOException("DAO-0002", "No valid session. Please authenticate first.");
		}
		
		/*
		 * Use the sessionId and temple code to fetch info about the temple
		 */
		String url = "http://www.dev.usys.org/familytree/v1/person/";
		url += id;
		url += "?sessionId=" + this.getSession().getId();
		
		log.debug(url);
		// refresh the client
		client = new HttpClient();
		
		// use the get method
		GetMethod method = new GetMethod(url);
		try {
			client.executeMethod(method);
		} catch (HttpException e) {
			// TODO swallow HttpException and throw a DAOException
			log.error(e);
		} catch (IOException e) {
			// TODO swallow IOException and throw a DAOException
			log.error(e);
		}		
		
		Person person = null;
		
		if( method.getStatusCode() == 200 )
		{
			InputStream responseBody = null;
			try {
				responseBody = method.getResponseBodyAsStream();
			} catch (IOException e) {
				// TODO swallow IOException and throw a DAOException
				e.printStackTrace();
			}
			
	
			// parse XML
			String xml = parseInputStream(responseBody);
			log.debug(xml);
			
			SAXParser sax = null;
			
			try {
				sax = SAXParserFactory.newInstance().newSAXParser();
			} catch (ParserConfigurationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (SAXException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			try {
				InputSource source = new InputSource(new StringReader(xml));
				sax.parse(source, parser);

			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}
		
		person = parser.getPerson();
		log.debug("Person=" + person);
		
		if( person == null )
		{
			throw new DAOException("DAO-0001", method.getStatusLine().toString() + " while trying to reach http://www.dev.usys.org" + method.getPath() + " with valid session ID.");
		}

		return person;				
		
	}


}
